home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / J4FTUT02.ZIP / temp / techtut / tut2 / ptech2.txt next >
Encoding:
Text File  |  1997-09-10  |  8.1 KB  |  191 lines

  1.  
  2.  ========================================================================  
  3.                    Just4Fun Productions presents:
  4.    
  5.                       Part 2 of: the Techtutor
  6.                        (Topics never covered)  
  7.  
  8.  
  9.                       Aliens, Objects and Things
  10.  ========================================================================  
  11.                   
  12. Introduction
  13. ============-
  14.  
  15. Aliens, now here's a challenge. Every alien/enemy or object can have its own AI code, so I'm not explaining how to make them move, or animate, I will just explain procedures or functions to create them and use them.
  16. For a small, but not complete example, look at TECH02.PAS.
  17. In this text I mainly talk about aliens, but objects and bonus things should 
  18. be seen as static aliens (or maybe moving, but at least they can use the 
  19. same kind of code).
  20.  
  21. ----------------------------------------------------------------
  22. For contacting Just4Fun or me (PeeBee) use the following ways:
  23.  
  24. Internet : http://people.zeelandnet.nl/rpb
  25. Email    : just4fun@zeelandnet.nl
  26.  
  27. SnailMail: P.Bestebroer
  28.      Anthony Edenlaan 28
  29.      4463 JC  Goes (Zld.)
  30.       Holland
  31.  
  32. ICQ      : 2309110 (probably fastest way to contact me)
  33. ----------------------------------------------------------------
  34.  
  35. OOP s?
  36. ======-
  37.  
  38. I program my aliens by using Object Oriented Programming. This is something 
  39. I'm not going to explain here since it's not that easy to understand, or to 
  40. explain. Just buy a good book about OOP and read it carefully.
  41. Some people might think that OOP can not really be used for games but that's 
  42. where they are wrong. OOP let's you create a base-alien object, and then you 
  43. can simply add new aliens by creating objects based on the "base-alien". The 
  44. "base-alien" should only have things like drawing it, collision-detections, 
  45. initialisation for bullets, player-collision checking and a few other 
  46. things. Since most alien-types will act different, you can easily             
  47. create a new object-type for a certain allien. That object will then 
  48. incorporate the movement, AI, animation and other SPECIFIC things              
  49. about that alien type. Let's use an example.
  50. Most people favourite game is QUAKE, Quake has alot of different monsters, 
  51. but they don't all "act" the same way. Let's see what we could do as a 
  52. BASE-ALIEN object, and then I'll gave an example.
  53.  
  54.  
  55. Collision Detection
  56. ===================-
  57.  
  58. All aliens, will need to be stopped when walking against a wall. Now you'r 
  59. game could have "ghost" aliens wich are able to walk thru a wall, in that 
  60. case you'll just write 1 extra collision routine specific for the "ghost" 
  61. type aliens. But all other aliens can simply use the Base_Alien collision 
  62. detections
  63.  
  64. Searching
  65. =========-
  66.  
  67. All aliens will have to "SEE" if the player is in their sight. This 
  68. procedure is the same for all aliens, so it can be put inside the 
  69. Base_Alien. Just take a look at the enemies in Quake. If you sneak             
  70. from behind every type will just be walking a certain path. This is the 
  71. Base_Alien's routine wich can be called from all alien-types.
  72.  
  73. Falling, Jumping, Diving
  74. ========================-
  75.  
  76. Most aliens will fall if there's no ground below them. So the "Gravity" 
  77. procedures should be put in the Base_Alien. Note, however, that not all 
  78. aliens will "walk" some might be flying in wich case you won't have to call 
  79. the procedure. There are a lot more procedures wich can be put into the
  80. Base_Alien, but it depands on you'r game. As you might have noticed the 
  81. Base_Alien contains alot of procedures that have to do with "game-physics" 
  82. (ie. falling, looking, colliding ) that's exactly what should be in the 
  83. Base_Alien! the physic-laws of you'r game-environment.
  84.  
  85.  
  86. What's left for the other objects?
  87.  
  88. Well the alien-specific object will include things like:
  89.  * Shooting
  90.  * Animating (but this could be done in the Base_Alien aswell)
  91.  * Thinking (only if you use multiple AI's )
  92. and a lot of other things wich you need for only a few aliens..
  93.  
  94. {=-=-= Example =-=-=}
  95. TYPE   Pactor = ^Tactor;   { this is the Base_Alien code }
  96.        Tactor = object
  97.          Xposition,
  98.          Yposition    : integer;          { Xposition and Yposition       }
  99.          Xstart,Ystart: integer;          { X+Y start positions           }
  100.          Xspeed,
  101.          Yspeed       : real;              { Xspeed and Yspeed             }
  102.          xdir,ydir    : shortint;          { direction of movement         }
  103.          AI           : byte;              { AI state                      }
  104.          energy       : byte;              { energy of the alien           }
  105.          ID           : byte;              { alien number                  }
  106.          killed       : boolean;           { if actor is out of loop       }
  107.          I_frame,
  108.          f_Frame,                          { Animation frame data          }
  109.          a_Frame,
  110.          c_frame,
  111.          t_frame,
  112.          s_Frame     : byte;
  113.          n_frame     : shortint;
  114.          cycle       : boolean;
  115.  
  116.         CONSTRUCTOR INIT(xp,yp:integer);       { initialisation of alien  }
  117.          PROCEDURE sub_init;           Virtual; { for some extra init     }
  118.          PROCEDURE animate;            Virtual; { animate the images      }
  119.          FUNCTION see_if_hit:boolean;  Virtual; { see if hit by bullet    }
  120.          FUNCTION PLAYER_HIT:boolean;  Virtual; { see if hitting the player}
  121.          PROCEDURE putit;              Virtual;  { draw the alien          }
  122.          PROCEDURE movement;           Virtual;  { moving the alien        }
  123.          PROCEDURE acting;             Virtual;  { the AI procedure        }
  124.          DESTRUCTOR DONE;                       { For erasing alien        }
  125.       END;
  126.  
  127.    {=-=-= Extra objects could look like this: =-=-=-=}
  128.     TYPE  Psoldier = ^Tsoldier;   { A ground-walker (ie. soldier) }
  129.           Tsoldier = object(TACTOR)    { is based on the Base_Actor    }
  130.            PROCEDURE SUB_INIT; Virtual; { For specific initialisations }
  131.            PROCEDURE Acting; VIRTUAL;   { For the AI of the soldiers   }
  132.           END;
  133.     
  134. {=-=-= End of Example =-=-=}
  135.  
  136. How to set it up
  137. ================-
  138.  
  139. Just like bullets, you could allso use a linked-list for the aliens but I 
  140. usually just use an array of pointers. You'll first need a procedure wich will create a new alien by specifying wich alien-type you need. This could look something like this:
  141. {=-=-= Example =-=-=-=}
  142. FUNCTION AddActor(ai:byte;xp,yp:integer):Pactor;
  143. BEGIN
  144.  Addactor:=NIL;
  145.  case ai of
  146.     1 : addActor:=new(PGroundWalker,init(xp,yp)); { first alien type }
  147.     2 : addActor:=new(PFlying,init(xp,yp));       { second alien type }
  148.     { ... }
  149.  end;
  150. END;
  151. {=-=-= End of Example =-=-=-=}
  152.  
  153.  
  154. To process the aliens, you'll simply make a procedure that will go thru the list, and call the "main_acting" procedure of the alientype. 
  155.  
  156. {=-=-= Example =-=-=-=}
  157. PROCEDURE DoActors;
  158. VAR i       : word;
  159. BEGIN
  160.  if actor_a=0 then exit;        { no actors to process                   }    
  161.  i:=1;                          { start with first actor                 }
  162.  while i<actor_a+1 do begin
  163.    with act[i]^ do begin
  164.         if not KILLED then begin        { if alien still lives,          }
  165.            Acting;      { make it act (moving, shooting, etc...)         }
  166.            PutIt;       { and draw it.                                   }
  167.         end;
  168.    end;
  169.    inc(i);              { go to next actor in the array                  }
  170.  end;
  171. END;
  172. {=-=-= End of Example =-=-=-=}
  173.  
  174. The "Main_Acting" procedure will handle the things like:
  175.  * Movement, usually a procedure in the Base_alien object
  176.  * Collision checking on walls, player(s), and other aliens.
  177.  * and the alien-specific procedures and AI.
  178.  
  179.  
  180. Examples?
  181. =========-
  182.  
  183. As I said at the beginning of this text, I won't go into procedure details for creating AI routines, there are other tutors and documents for that..
  184. if you want to see some good AI routines, just take a look at Quake-C source codes, maybe you learn a few nice tricks from that...
  185. I hope you learned something, and keep coding!
  186.  
  187. Next Tech topic: HighScores
  188.  
  189.  
  190. PeeBee - September 10th '97
  191.